#include "Mario.h"
#include "ResourceManager.h"

CMario::CMario() : CObject()
{
	m_pos = D3DXVECTOR2(300, 350);
	m_direct = 1;
	//m_status = MARIO_ON_AIR;
	m_velocity.x = 0;
	m_velocity.y = 10;
}

void CMario::Init()
{
	m_sprite = CResourceManager::GetInstance()->GetSprite(RESOURCE__GAMEPLAY_MARIO_KID);
	CObject::Init();
	CObject::UpdateBond();
}

void CMario::Update(float gameTime, CInput* input)
{
	// Xu ly input to gia toc va van toc
		if (input ->KeyDown(DIK_RIGHT))
		{
			//// Set huong
			//if (m_direct == -1)
			//	m_velocity.x = 0;
			//m_direct = 1;
			//// Set status
			///*if (m_status != MARIO_JUMP)
			//	m_status = MARIO_RUN;*/

			//// Set gia toc
			//m_acceleration.x = ACCELERATION_X;
			//// Gioi han van toc
			//if (m_velocity.x >= VELOCITY_X)
			//{
			//	m_acceleration.x = 0;
			//} // Neu bam RIGHT KEY
			m_velocity.x = 10;
		} else if (input->KeyDown(DIK_LEFT))
		{
		//	if (m_direct == 1)
		//		m_velocity.x = 0;
		//	m_direct = -1;
		//	/*if (m_status != MARIO_JUMP)
		//		m_status = MARIO_RUN;*/
		//	m_acceleration.x = (float)m_direct * ACCELERATION_X;
		//	if (m_velocity.x <= m_direct * VELOCITY_X)
		//	{
		//		m_acceleration.x = 0;
		//	} // Neu bam RIGHT KEY
		//}
		//else
		//{
		//	if (m_velocity.x != 0)
		//		m_acceleration.x = - 1.0f * m_direct * ACCELERATION_X;
		//	if ((m_direct * m_velocity.x) <= 0)
		//	{
		//		m_acceleration.x = 0;
		//		m_velocity.x = 0;
		//	}
			m_velocity.x = -10;
		}


		if (input->KeyPressed(DIK_A))
		{
			if (m_status != MARIO_JUMP) // Dang nhay thi ko nhay nua
			{
				m_status = MARIO_JUMP;
				m_velocity.y = -10;
			}
		}

		if (input->KeyUp(DIK_LEFT))
		{
			m_velocity.x = 0;
		} 
		else
		{
		}

	// Xu ly trang thai
	if ((m_velocity.x == 0) && (m_velocity.y == 0))
	{
		m_status = MARIO_STAND;
	} else if (m_velocity.y != 0)
	{
		m_status = MARIO_JUMP;
	} else if((m_velocity.x != 0) && (m_velocity.y == 0))
	{
		m_status = MARIO_RUN;
	}


	// Xu ly status thanh animate
	UpdateAnimation(gameTime, input);

	// Phai co dc van toc va gia toc
	//CObject::UpdatePosition(gameTime, input); // Update vi tri
}

void CMario::Draw(LPD3DXSPRITE spriteHandler, CCamera* camera)
{
	D3DXMATRIX temp, matrix, temp2;
	spriteHandler->GetTransform(&temp);

	D3DXMatrixTransformation2D(&matrix, &D3DXVECTOR2(m_sprite->GetFrameWidth()/2, m_sprite->GetFrameHeight()/2), 0.0f, &D3DXVECTOR2((float)m_direct, 1.0f), NULL, 0.0f, &D3DXVECTOR2(0,0));
	
	D3DXMatrixMultiply(&matrix, &temp, &matrix);
	spriteHandler->SetTransform(&matrix);
	CObject::Draw(spriteHandler, camera);
	spriteHandler->SetTransform(&temp);
}

void CMario::UpdateAnimation(float gameTime, CInput* input)
{
	if (m_velocity.x == 0 && m_velocity.y == 0) // STAND
	{
		m_sprite->SetCurrentFrame(0);
		m_sprite->SetTimeAnimation(0);
		m_sprite->Update(gameTime);
	} else if (m_velocity.x != 0 && m_velocity.y == 0) // RUN, khong JUMP
	{
		m_sprite->SetTimeAnimation(0.1f);
		m_sprite->Update(gameTime, 0, 1);
	} else if (m_velocity.y != 0) // JUMP
	{
		m_sprite->SetCurrentFrame(2);
		m_sprite->SetTimeAnimation(0);
		m_sprite->Update(gameTime);
	}
}

OBJECT_TYPE CMario::GetType()
{
	return OBJECT_MARIO;
}